package tools;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import application.CanvasView;

public class TextTool extends Tool
{
	// Components
	protected CanvasView canvas;
	protected Point startingPosition;
	protected StringBuffer text;
	protected Font font = new Font("Serif", Font.BOLD, 24);
	
	// Constructor
	 public TextTool(CanvasView c)
	 {
		 if( c != null )
			 canvas = c;
		 else
			 throw new IllegalArgumentException();
	 }
	 
	 /************Event handlers***************/
	 /*  
	  * Returns focus to the drawing canvas and stores the starting location for
	  * the text display.
	  * @see tools.Tool#mousePressed(java.awt.event.MouseEvent)
	  */
	 public void mousePressed(MouseEvent e)  
	 {
		 canvas.requestFocus();
		 startingPosition = e.getPoint();
		 Graphics iBGraphics = canvas.getimageBufferGraphics();
		 iBGraphics.setFont(font);
		 text = new StringBuffer();
	  }

	 /*  
	  * Adds a character to the string buffer
	  * 
	  * @see tools.Tool#keyPressed(java.awt.event.KeyEvent)
	  */
	 public void keyPressed(KeyEvent e)  
	 {
		 char nextChar = e.getKeyChar(); 
		 text.append(nextChar); 
		 Graphics iBGraphics = canvas.getimageBufferGraphics();
		 iBGraphics.drawString(text.toString(), startingPosition.x, startingPosition.y); 
		 canvas.repaint();
	 }
}
